package Before826.AutoJudge;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class getStudent {
    public List<Student> GetStudent (File file){
        List<Student> students = new ArrayList<>() ;
        if (file.isDirectory() ) {
            File[] files = file.listFiles();
            if (files != null){
                for(File f : files){

                    List<Student> childStudents = GetStudent(f);
                    if (childStudents != null) {
                        for (Student childStudent : childStudents) {
                            students.add(childStudent);
                        }
                    }
                }
                return students;
            }
        }
        if (file.isFile() && !file.getName().equalsIgnoreCase("标准答案.txt") && !file.getName().equalsIgnoreCase("第一册考试成绩.txt")  && file.getName().toString().endsWith(".txt")) {
            try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
                Student stu = new Student();
                List<String> list = new ArrayList<>();
                String line;
                while ((line = reader.readLine()) != null) {
                    list.add(line);
                }
                if (!list.isEmpty() ) {
                    stu.setAnswer(list);
                    stu.setName(file.getName().replace(".txt", ""));
                    students.add(stu);
                    return students;
                }
            } catch (IOException e) {
                System.out.println("读取文件时发生错误" + file.getName());
                e.printStackTrace();
                return new ArrayList<>();
            }
        }
        return null;
    }
}
